package com.magical.service.serviceImp;

import com.magical.dao.FilesMapper;
import com.magical.entity.Files;
import com.magical.service.FilesService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class FilesServiceImpl implements FilesService {
    @Resource
    private FilesMapper filesMapper;


    @Override
    public int upload(Files file) {
        return filesMapper.insertSelective(file);
    }

    @Override
    public Files findById(String id) {
        return filesMapper.selectByPrimaryKey(id);
    }

    @Override
    public Files findRecycleById(String id) {
        return filesMapper.selectRecycle(id);
    }

    @Override
    public void createUser(int id) {
        String path = System.getProperty("root.webapp")+"upload/"+String.valueOf(id);
        File f=new File(path);
        f.mkdir();
        Files file=new Files();
        file.setFileId(String.valueOf(id));
        file.setFileName(String.valueOf(id));
        file.setFileSize(0);
        file.setUserId(id);
        file.setFullPath(path);
        file.setFiletypeId(5);
        filesMapper.insertSelective(file);
    }

    @Override
    public int delete(String id) {
        return filesMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateFatherId(Files file) { return filesMapper.updateByPrimaryKeySelective(file); }

    @Override
    public void renameFile(String id,String newname) {
        Files temp=findById(id);
        File parent=new File(temp.getFullPath()).getParentFile();
        temp.setFileName(newname);
        temp.setFullPath(parent.getAbsolutePath()+"/"+newname);
        filesMapper.updateByPrimaryKeySelective(temp);
        int userId=temp.getUserId();
        if(temp.getFiletypeId()!=5)
            filesMapper.updateByPrimaryKeySelective(temp);
        else{
            List<Files> list=findSubfile(id);
            for(Files f:list)
                moveFiles(f,temp,userId);
        }
    }

    @Override
    public void deleteDirectory(String id) {
        Files temp=findRecycleById(id);
        if(temp.getFiletypeId()!=5){
            filesMapper.deleteByPrimaryKey(id);
            return;
        }
        List<Files> list=filesMapper.findDeletedSubfile(id);
        if(list.isEmpty()){
            filesMapper.deleteByPrimaryKey(id);
            return;
        }
        for(Files f:list)
            deleteDirectory(f.getFileId());
        filesMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void falseDelDirectory(String id) {
        Files temp=filesMapper.selectByPrimaryKey(id);
        if(temp.getFiletypeId()!=5){
            filesMapper.falseDele(1,id);
            return;
        }
        List<Files> list=findSubfile(id);
        if(list.isEmpty()){
            filesMapper.falseDele(1,id);
            return;
        }
        for(Files f:list)
            falseDelDirectory(f.getFileId());
        filesMapper.falseDele(1,id);
    }


    @Override
    public List<Files> vagueSearchFile(String value,int id) {
        List<Files> f= filesMapper.vagueSearchFile(value,id);
        //过滤掉文件夹 文件夹不显示
        List<Files> result=new ArrayList<>();
        for(Files x:f){
            if(x.getFiletypeId()!=5)
                result.add(x);
        }
        return result;
    }

    @Override
    public void moveFiles(Files file, Files to,int userId) {
        String newPath=to.getFullPath();
        if(file.getFiletypeId()!=5){
            file.setFullPath(newPath+"/"+file.getFileName());
            file.setFatherId(to.getFileId());
            filesMapper.updateByPrimaryKeySelective(file);
            return;
        }
        to.setFullPath(newPath+"/"+file.getFileName());
        List<Files> list=findSubfile(file.getFileId());
        for(Files f:list)
            moveFiles(f,to,userId) ;
        file.setFullPath(newPath+"/"+file.getFileName());
        filesMapper.updateByPrimaryKeySelective(file);
    }

    @Override
    public List<Files> findSubfile(String parentId) {
        return filesMapper.findSubfile(parentId);
    }

    @Override
    public List<Files> showFilesByType(int userId,int fileType) {
        Map<String, Object> data = new HashedMap();
        data.put("userId", userId);
        data.put("fileType", fileType);
        return filesMapper.findFilesByType(data);
    }

    @Override
    public List<Files> selectDelected(int userId) {
        List<Files> files = filesMapper.findDelected(userId);
        List<Files> results = new ArrayList<>();
        for(Files f1:files){
            int flag = 0;
            String fatherId = f1.getFatherId();
            for(Files f2:files){
                if(f2.getFileId().equals(fatherId)){
                    flag = 1;
                }
            }
            if(flag == 0) {
                results.add(f1);
            }
        }
        return results;
    }


    @Override
    public void falesdeleteById(int delected,String id){
        Files temp=filesMapper.selectByPrimaryKey(id);
        if(temp == null)
            temp=filesMapper.selectRecycle(id);
        if(temp.getFiletypeId()!=5){
            filesMapper.falseDele(delected,id);
            return;
        }
        List<Files> list=findSubfile(id);
        if(list.isEmpty()){
            filesMapper.falseDele(delected,id);
            return;
        }
        for(Files f:list)
            falesdeleteById(delected,f.getFileId());
        filesMapper.falseDele(delected,id);
    }
}
